// Created by WXX on 2021/10/22 19:42
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

class Solution {
public:
    int n;
    vector<bool> col, dg, udg;

    int totalNQueens(int _n) {
        n = _n;
        col = vector<bool>(n);
        dg = udg = vector<bool>(n * 2 - 1);

        return dfs(0);
    }

    // 返回方案数
    int dfs(int u) {
        if (u == n) return 1;

        int res = 0;
        for (int i = 0; i < n; i ++ ) {
            if (!col[i] && !dg[u - i + n - 1] && !udg[u + i]) {
                col[i] = dg[u - i + n - 1] = udg[u + i] = true;
                res += dfs(u + 1);
                col[i] = dg[u - i + n - 1] = udg[u + i] = false;
            }
        }
        return res;
    }
};

int main() {

    cout << Solution().totalNQueens(4) << endl;  // 2

    return 0;
}
